package com.skywolf.chem.match.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.skywolf.chem.common.config.data.DataSource;
import com.skywolf.chem.common.config.data.DataSourceNames;
import com.skywolf.chem.match.entity.UserCollectRecord;
import com.skywolf.chem.match.mapper.UserCollectRecordMapper;
import com.skywolf.chem.match.service.IUserCollectRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户收藏活动记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-09-27
 */
@Service
@AllArgsConstructor
public class UserCollectRecordServiceImpl extends ServiceImpl<UserCollectRecordMapper, UserCollectRecord> implements IUserCollectRecordService {

    private final UserCollectRecordMapper collectRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DataSource(DataSourceNames.TWO)
    public void collectMatch(Long userId, Long id) {
        UserCollectRecord record=this.getOne(new LambdaQueryWrapper<UserCollectRecord>().eq(UserCollectRecord::getUserId,userId).eq(UserCollectRecord::getMatchId,id));
        if(record==null){
            //没有收藏过去收藏
            record=new UserCollectRecord();
            record.setMatchId(id);
            record.setUserId(userId);
            this.save(record);
        }else{
            collectRecordMapper.deletedRealById(record.getId());
        }
    }
}
